﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using Md = Com.Ooohd.Model.Topic;
using DBUtility;
namespace Com.Ooohd.Bll.Topic
{
    public class ZS_TopicAlbum
    {
        public void Add(Md.ZS_TopicAlbum TopicAlbum)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection)) {
                ct.ZS_TopicAlbum.InsertOnSubmit(TopicAlbum);
                ct.SubmitChanges();
            }
        }

        public void Add(Md.ZS_TopicAlbum TopicAlbum,List<Md.ZS_TopicImg> imgList)
        {
            using (var con = DbHelperSQL.GetConnection(0))
            {
                var tran = con.BeginTransaction();
                var ct = new Md.ZS_TopicAlbumDataContext(con);
                ct.Transaction = tran;
                ct.ZS_TopicAlbum.InsertOnSubmit(TopicAlbum);
                ct.SubmitChanges();

                var ct2 = new Md.ZS_TopicImgDataContext(con);
                ct2.Transaction = tran;
                foreach (var item in imgList)
                {
                    item.AlbumID = TopicAlbum.ID;
                }
                ct2.ZS_TopicImg.InsertAllOnSubmit(imgList);
                ct2.SubmitChanges();

                tran.Commit();
            }

            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                ct.ZS_TopicAlbum.InsertOnSubmit(TopicAlbum);
                ct.SubmitChanges();
            }
        }

        public void Update(Md.ZS_TopicAlbum TopicAlbum)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                ct.ZS_TopicAlbum.Attach(TopicAlbum,true);
                ct.SubmitChanges();
            }
        }

        public void Delete(int ID)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                var st = ct.ZS_TopicAlbum.Single(a => a.ID == ID);
                ct.ZS_TopicAlbum.DeleteOnSubmit(st);
                ct.SubmitChanges();
            }
        }

        public void Delete(string Ids)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                string[] idarray = Ids.Split(',');
                var list = ct.ZS_TopicAlbum.Where(a => idarray.Contains(a.ID.ToString()));
                ct.ZS_TopicAlbum.DeleteAllOnSubmit(list);
                ct.SubmitChanges();
            }
        }

        public Md.ZS_TopicAlbum GetSingle(int ID)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                return ct.ZS_TopicAlbum.Single(a => a.ID == ID);
            }
        }

        public List<T> Query<T>(string sql, params object[] parameterValues)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                return ct.ExecuteQuery<T>(sql, parameterValues).ToList();
            }
        }

        public List<Md.ZS_TopicAlbum> Query(string condition, string orderby, int startIndex, int pageSize, ref int pageCount, params object[] param)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.ZS_TopicAlbum> TopicAlbumList = ct.ZS_TopicAlbum;
                if (!string.IsNullOrEmpty(condition))
                    TopicAlbumList = TopicAlbumList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    TopicAlbumList = TopicAlbumList.OrderBy(orderby);
                pageCount = TopicAlbumList.Count();
                return TopicAlbumList.Skip(startIndex).Take(pageSize).ToList();
            }
        }

        public List<Md.ZS_TopicAlbum> Query(string condition, string orderby, params object[] param)
        {
            using (var ct = new Md.ZS_TopicAlbumDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.ZS_TopicAlbum> TopicAlbumList = ct.ZS_TopicAlbum;
                if (!string.IsNullOrEmpty(condition))
                    TopicAlbumList = TopicAlbumList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    TopicAlbumList = TopicAlbumList.OrderBy(orderby);
                return TopicAlbumList.ToList();
            }
        }

        public Md.ZS_TopicAlbum CreateModel()
        {
            return new Md.ZS_TopicAlbum();
        }
    }
}
